#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "filewidget.h"
#include "midi.h"
#include "trackplayer.h"
#include <QTimer>
#include <QtConcurrentRun>
#include <QFuture>
#include <Qtimeline>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QThreadPool::globalInstance()->setMaxThreadCount(10);
    ui->setupUi(this);
    runs=false;
    playback = false;
    //set up fileselect widget
    fileselect = new fileWidget;
    connect(fileselect,SIGNAL(accepted()),this,SLOT(getMidiFile()));
    //--

    //set up graphics view
    scene= new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    QPixmap bg;
    bg.load("bg.png");
    bg.scaled(QSize(300,400),Qt::IgnoreAspectRatio,Qt::FastTransformation);
    Pixmap *mybg=new Pixmap(bg);

    scene->addItem(mybg);

    for(int i=0;i<7;i++)
    {
        QPixmap pixa(36,10);
        pixa.fill(QColor(0,0,0,100));

        Pixmap *pix=new Pixmap(pixa);
        int height = 50;

        QPropertyAnimation *nAni  = new QPropertyAnimation(pix,"pos");
        nAni->setDuration(1000);
        nAni->setStartValue(QPointF(i,height));
        nAni->setEndValue(QPointF(i*40+12,height+315));
        scene->addItem(pix);
        nAni->start();
    }
    //--


    midiOutOpen(&handle,(UINT)-1,0,0,CALLBACK_NULL);
}

MainWindow::~MainWindow()
{
    midiOutClose(handle);
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    fileselect->show();
}
void MainWindow::getMidiFile()
{

    midifile = new midi(fileselect->getMidiPath());
    ui->textBrowser->setText(fileselect->getMidiName());
}
void MainWindow::on_playmidiButton_clicked()
{
    ui->graphicsView->setFocus();
    if(playback)
    {
        for(int i=0;i<midifile->midiTrack.size();i++)
        {
            playback_player[i]->terminate();
        }
    }
    if(runs)
        gamingtrack->terminate();
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        if(runs)
            myplayer[i]->terminate();
    }
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        myplayer[i] = new threadplay(midifile->midiTrack[i],handle);
    }

    gamingtrack = new notegen(midifile->midiTrack[0],handle);
    connect(gamingtrack,SIGNAL(notetoplay(long)),this,SLOT(notetoscn(long)));
    connect(gamingtrack,SIGNAL(notetoplay(long)),this,SLOT(getforward(long)));

    gamingtrack->start(QThread::TimeCriticalPriority);
    Sleep(1000);
    for(int i=1;i<(int)midifile->midiTrack.size();i++)
    {
        myplayer[i]->start(QThread::TimeCriticalPriority);
    }
    runs = true;
     QThread::currentThread()->setPriority(QThread::TimeCriticalPriority) ;
}
void MainWindow::notetoscn(long msg)
{
    if((msg&0xff)==0x90)
    {
        int col=(msg%7)*40+15;
        int height = 50;
        QPixmap pixa;



        if(col==3*40+15)
        {
            pixa.load("r.png");

        }else
        {

            pixa.load("B.png");

        }
        Pixmap *pix=new Pixmap(pixa);
        QPropertyAnimation *nAni  = new QPropertyAnimation(pix,"pos");

        nAni->setDuration(1000);
        nAni->setStartValue(QPointF(col,height));
        nAni->setEndValue(QPointF(col,height+300));
        connect(nAni,SIGNAL(finished()),pix,SLOT(deleteLater()));
        scene->addItem(pix);
        nAni->start();

    }

}

void MainWindow::changeKey()
{
    long msg=keylist.dequeue();
    if((msg&0xff)==0x90)
    {
        int keyno=msg%7;
        keys[keyno]=msg;
    }

}
void MainWindow::getforward(long msg)
{
    keylist.enqueue(msg);
    QTimer::singleShot(800,this,SLOT(changeKey()));
}

void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_S)
    {

        midiOutShortMsg(handle,keys[0]);

    }
    if(e->key()==Qt::Key_D)
    {
        midiOutShortMsg(handle,keys[1]);


    }
    if(e->key()==Qt::Key_F)
    {
        midiOutShortMsg(handle,keys[2]);

    }
    if(e->key()==Qt::Key_Space)
    {
        midiOutShortMsg(handle,keys[3]);
    }
    if(e->key()==Qt::Key_J)
    {
        midiOutShortMsg(handle,keys[4]);
    }
    if(e->key()==Qt::Key_K)
    {
        midiOutShortMsg(handle,keys[5]);
    }
    if(e->key()==Qt::Key_L)
    {
        midiOutShortMsg(handle,keys[6]);
    }

}




void MainWindow::on_speed2_clicked()
{
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        for(int j=0;j<midifile->midiTrack[i].trknotes.size();j++)
        {
            long t =midifile->midiTrack[i].trknotes[j].deltaTime;
            midifile->midiTrack[i].trknotes[j].deltaTime = t*3;
        }
    }
}

void MainWindow::on_speed3_clicked()
{
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        for(int j=0;j<midifile->midiTrack[i].trknotes.size();j++)
        {
            long t =midifile->midiTrack[i].trknotes[j].deltaTime;
            midifile->midiTrack[i].trknotes[j].deltaTime = t*2;
        }
    }
}

void MainWindow::on_speed4_clicked()
{
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        for(int j=0;j<midifile->midiTrack[i].trknotes.size();j++)
        {
            long t =midifile->midiTrack[i].trknotes[j].deltaTime;
            midifile->midiTrack[i].trknotes[j].deltaTime = t*1;
        }
    }
}


void MainWindow::on_playback_clicked()
{
    if(runs)
        gamingtrack->terminate();
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        if(runs)
            myplayer[i]->terminate();
    }

    if(playback)
    {
        for(int i=0;i<midifile->midiTrack.size();i++)
        {
            playback_player[i]->terminate();
        }
    }
    for(int i=0;i< (int)midifile->midiTrack.size();i++)
    {
        playback_player[i] = new threadplay(midifile->midiTrack[i],handle);
    }
    for(int i=0;i<(int)midifile->midiTrack.size();i++)
    {
        playback_player[i]->start(QThread::TimeCriticalPriority);
    }
    playback=true;
}
